﻿using DADAL;

namespace DABLL
{
    public class Device
    {
        /// <summary>
        /// 波特率数组
        /// </summary>
        public static List<int> BaudrateList = new List<int> {1200,2400,4800,9600,14400,19200,38400,57600,115200};


        /// <summary> 添加设备
        /// 
        /// </summary>
        /// <param name="obj"></param>
        public static Response AddDevice(DeviceModel obj)
        {
            Response response = Response.DefaultResponse();
            bool res = DeviceModel.RecordExistenceWithName(obj.Name);
            if (res)
            {
                response.Msg = string.Format("【{0}】设备已存在", obj.Name);
                return response;
            }

            try
            {
                obj.SlaveAddress = Encryption.DesEncrypt(obj.SlaveAddress,SqlSugarHelper.EncryptionKey);
            }
            catch (Exception)
            {
                response.Msg = "请重新输入设备地址";
                return response;
            }

            List<DeviceModel> list = new List<DeviceModel>();
            list.Add(obj);
            int result = SqlSugarHelper.Insertable(list);
            if (result > 0)
            {
                response.Code = 200;
                response.Msg = string.Format("已添加【{0}】设备", obj.Name);
            }
            else
            {
                response.Msg = string.Format("设备添加失败");
            }

            return response;
        }
        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static Response DeleteDevice(int number)
        {
            Response response = Response.DefaultResponse();
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<DeviceModel> list = SqlSugarHelper.Inquireable<DeviceModel>(where);
            if (list.Count > 0)
            {
                int deleteRes = SqlSugarHelper.Deleteable(list);
                if (deleteRes > 0)
                {
                    response.Code = 200;
                    response.Msg = string.Format("设备已删除");
                }
                else
                {
                    response.Msg = string.Format("设备删除失败");
                }
                return response;
            }
            else
            {
                response.Msg = string.Format("【{0}】设备不存在，无法删除", number);
            }
            return response;
        }

        /// <summary>
        /// 设备列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        public static List<DeviceModel> DeviceList(int page, int pageSize, ref int totalCount)
        {
            List<DeviceModel> list = SqlSugarHelper.InquireableToPageList<DeviceModel>(null, page, pageSize, ref totalCount);
            return list;
        }

        /// <summary>
        /// 根据设备编号查询设备
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static DeviceModel? DeviceWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<DeviceModel> list = SqlSugarHelper.Inquireable<DeviceModel>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }
    }
}
